/*
 * @Author: daifen
 * @Date: 2020-10-28 15:56:00
 * @LastEditors: daifen
 */

var FileData = require("../fileData/fileData");

/**
 * 增加
 */
function insertFileData(data) {

    var fileData = new FileData(data);
    return new Promise(resolve => {
        fileData.save(function (err, res) {

            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }

        });
    })

}

/**
 * @Desc: 按条件模糊查找10条数据
 * @Author: daifen
 * @Date: 2020-10-28 18:10:11
 * @param {*} data {}
 * @return {*}
 */
function getFileData(data) {

    return new Promise(resolve => {
        //有值时进入
        var condition;
        //利用正则模糊查询 
        const reg = new RegExp(data.fileName, 'i')
        if (typeof (data.fileName) != "undefined") { 
            condition = {
                "fileName":  {"$regex":data.fileName
            } , "delFlag": "0"
            };                 //条件
        } else {

            condition = {
                "delFlag": "0"
            };
        } 

        var pageSize = data.pageSize;                   //一页多少条
        var currentPage = data.pageNum;                //当前第几页 

        var skipnum = (currentPage - 1) * pageSize;   //跳过数

        FileData.find(condition).sort({createTime: -1}).skip(skipnum).limit(pageSize).exec(function (err, res) {
            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }
        })
    })

}

/**
 * @Desc: 删除
 * @Author: daifen
 * @Date: 2020-10-28 18:10:44
 * @param {*} data {'username' : 'Tracy McGrady'}
 * @return {*}
 */
function delFileData(data) {
    var wherestr = data;
    return new Promise(resolve => {
        FileData.remove(wherestr, function (err, res) {
            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }
        })
    })

}

/**
 * @Desc: one为 false单个更新 每次只能更新一条数据 true 全部更新
 * @Author: daifen
 * @Date: 2020-10-28 18:12:58
 * @param {*} wherestr = {'username' : 'Tracy McGrady'}; updatestr = {'userpwd': 'zzzz'};
 * @return {*}
 */
function updateFileData(olddata, newdata, one) {
    var wherestr = olddata;
    var updatestr = newdata;
    let onestr = one;
    return new Promise(resolve => {
        FileData.update(wherestr, updatestr, { multi: onestr }, function (err, res) {
            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }
        })
    })

}

/**
 * @Desc: 获取总数
 * @Author: daifen
 * @Date: 2020-10-29 16:34:08
 * @param {*}
 * @return {*}
 */
function getFileDataCount(data) {

    console.log(data)
    return new Promise(resolve => {

        var condition;
        //利用正则模糊查询 
        const reg = new RegExp(data.fileName, 'i')
        if (typeof (data.fileName) != "undefined") { 
            condition = {
                "fileName":  {"$regex":data.fileName
            } , "delFlag": "0"
            };                 //条件
        } else {

            condition = {
                "delFlag": "0"
            };
        } 

        FileData.countDocuments(condition, function (err, res) {
            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }
        })
    })
}

/**
 * @Desc: 无效分析
 * @Author: daifen
 * @Date: 2020-11-02 14:31:36
 * @param {*}
 * @return {*}
 */
function invalidResult(olddata, newdata) {

    return new Promise(resolve => {
        FileData.update(olddata, newdata, function (err, res) {
            if (err) {
                resolve(err);
                return err
            }
            else {
                resolve(res);
                return res
            }
        })
    })

}


export {
    insertFileData,
    getFileData,
    delFileData,
    updateFileData,
    getFileDataCount,
    invalidResult
}
